
package com.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.exceptions.ServiceException;
import com.demo.entity.SysRole;
import com.demo.entity.SysUser;
import com.demo.mapper.SysRoleMapper;
import com.demo.service.SysRoleService;
import com.demo.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * serviceImpl
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper,SysRole> implements SysRoleService {

    @Resource
    private SysUserService sysUserService;

    @Override
    public void removeRole(Integer roleId) {
        List<SysUser> sysUsers = sysUserService.getUserByRole(roleId);
        if(!sysUsers.isEmpty()){
            List<String> mobiles = sysUsers.stream().map(SysUser::getMobile).collect(Collectors.toList());
            String mobile = StringUtils.join(mobiles, "，");
            throw new ServiceException("用户"+mobile+"已绑定该角色，请先解绑再删除");
        }
        this.removeById(roleId);
    }
}
